package cn.itsource.ymcc.consumer;

import cn.itsource.ymcc.constant.Constants;
import cn.itsource.ymcc.constant.OrderStatusConstants;
import cn.itsource.ymcc.constant.PayConstants;
import cn.itsource.ymcc.constant.RConst;
import cn.itsource.ymcc.domain.CourseOrderItem;
import cn.itsource.ymcc.domain.CourseSummary;
import cn.itsource.ymcc.domain.CourseUserLearn;
import cn.itsource.ymcc.mapper.CourseSummaryMapper;
import cn.itsource.ymcc.mapper.CourseUserLearnMapper;
import cn.itsource.ymcc.openfeign.OrderOpenfeign;
import cn.itsource.ymcc.result.JsonResult;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import springfox.documentation.spring.web.json.Json;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

@Component
@RocketMQMessageListener(
        consumerGroup = "CoursePayConsumer",
        topic = PayConstants.TOPIC_PAY_ORDER,
        selectorExpression = PayConstants.TAGS_PAY_ORDER
)
@Slf4j
public class CoursePayConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private OrderOpenfeign orderOpenfeign;
    @Autowired
    private CourseSummaryMapper summaryMapper;
    @Autowired
    private CourseUserLearnMapper courseUserLearnMapper;

    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            byte[] body = messageExt.getBody();
            String orderNo = new String(body, Constants.UTF8);
            JsonResult orderItemByOrderNo = orderOpenfeign.getOrderItemByOrderNo(orderNo);
            List<CourseOrderItem> list = JSON.parseArray(orderItemByOrderNo.getData().toString(), CourseOrderItem.class);
            list.forEach(item->{
                //添加用户购买记录
                CourseUserLearn courseUserLearn = new CourseUserLearn();
                courseUserLearn.setLoginId(item.getUserId());
                courseUserLearn.setState(RConst.Num.INT_0);
                courseUserLearn.setCourseId(item.getCourseId());
                courseUserLearn.setCourseOrderNo(item.getOrderNo());
                courseUserLearn.setCreateTime(new Date());
                courseUserLearn.setStartTime(new Date());
                Long end =  System.currentTimeMillis()+(1000*60*60*24*365*10);
                courseUserLearn.setEndTime(new Date(end));
                courseUserLearnMapper.insert(courseUserLearn);
                //修改课程销量
                CourseSummary courseSummary = summaryMapper.selectById(item.getCourseId());
                courseSummary.setSaleCount(courseSummary.getSaleCount() + RConst.Num.INT_1);
                summaryMapper.updateById(courseSummary);
            });
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }
}
